package com.ruoyi.energy.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.energy.domain.MjGuardPermission;
import com.ruoyi.energy.domain.MjPermissionDevice;
import com.ruoyi.energy.mapper.MjGuardPermissionMapper;
import com.ruoyi.energy.mapper.MjPermissionDeviceMapper;
import com.ruoyi.energy.service.IMjGuardPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 门禁权限监控Service业务层处理
 *
 * @author cjie
 * @date 2021-07-29
 */
@Service
public class MjGuardPermissionServiceImpl implements IMjGuardPermissionService {
    @Autowired
    private MjGuardPermissionMapper mjGuardPermissionMapper;
    @Autowired
    private MjPermissionDeviceMapper mjPermissionDeviceMapper;

    /**
     * 查询门禁权限监控
     *
     * @param id 门禁权限监控ID
     * @return 门禁权限监控
     */
    @Override
    public MjGuardPermission selectMjGuardPermissionById(String id) {
        MjGuardPermission mjGuardPermission = mjGuardPermissionMapper.selectMjGuardPermissionById(id);
        List<String> deviceIdList = mjPermissionDeviceMapper.selectDeviceIdByPermissionId(mjGuardPermission.getId());
        mjGuardPermission.setDeviceIdList(deviceIdList);
        return mjGuardPermission;
    }

    /**
     * 查询门禁权限监控列表
     *
     * @param mjGuardPermission 门禁权限监控
     * @return 门禁权限监控
     */
    @Override
    public List<MjGuardPermission> selectMjGuardPermissionList(MjGuardPermission mjGuardPermission) {
        return mjGuardPermissionMapper.selectMjGuardPermissionList(mjGuardPermission);
    }

    /**
     * 新增门禁权限监控
     *
     * @param mjGuardPermission 门禁权限监控
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMjGuardPermission(MjGuardPermission mjGuardPermission) {
        mjGuardPermission.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        mjGuardPermission.setCreateTime(DateUtils.getNowDate());
        mjGuardPermission.setCreateBy(SecurityUtils.getUsername());

        linkDevice(mjGuardPermission);
        return mjGuardPermissionMapper.insertMjGuardPermission(mjGuardPermission);
    }

    private void linkDevice(MjGuardPermission mjGuardPermission) {
        List<String> deviceIds = mjGuardPermission.getDeviceIdList();
        if(deviceIds != null && deviceIds.size() >0){
            deviceIds.forEach(deviceId->{
                MjPermissionDevice permissionDevice = new MjPermissionDevice();
                permissionDevice.setDeviceId(deviceId);
                permissionDevice.setPermissionId(mjGuardPermission.getId());

                mjPermissionDeviceMapper.insertMjPermissionDevice(permissionDevice);
            });
        }
    }

    /**
     * 修改门禁权限监控
     *
     * @param mjGuardPermission 门禁权限监控
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMjGuardPermission(MjGuardPermission mjGuardPermission) {
        mjGuardPermission.setUpdateTime(DateUtils.getNowDate());
        mjGuardPermission.setUpdateBy(SecurityUtils.getUsername());
        List<String> deviceIds = mjGuardPermission.getDeviceIdList();
        if(deviceIds != null && deviceIds.size() >0){
            mjPermissionDeviceMapper.deleteByPermissionId(mjGuardPermission.getId());
        }

        linkDevice(mjGuardPermission);
        return mjGuardPermissionMapper.updateMjGuardPermission(mjGuardPermission);
    }

    /**
     * 批量删除门禁权限监控
     *
     * @param ids 需要删除的门禁权限监控ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteMjGuardPermissionByIds(String[] ids) {
        Arrays.stream(ids).forEach(id->{
            mjPermissionDeviceMapper.deleteByPermissionId(id);
        });
        return mjGuardPermissionMapper.deleteMjGuardPermissionByIds(ids);
    }

    /**
     * 删除门禁权限监控信息
     *
     * @param id 门禁权限监控ID
     * @return 结果
     */
    @Override
    public int deleteMjGuardPermissionById(String id) {
        return mjGuardPermissionMapper.deleteMjGuardPermissionById(id);
    }
}
